home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bitstrg.zip / LBITSTRG.C < prev    next >
C/C++ Source or Header  |  1989-06-05  |  4KB  |  169 lines

  1. /*
  2.     file : bitstrg.c            June  1, 1989
  3.  
  4.     programm to test bit manipulation procedures
  5.  
  6. */
  7.  
  8. #define debug
  9.  
  10. /* standard include */
  11.  
  12. #include <stdio.h>
  13.  
  14. /* specific include */
  15.  
  16. #include "bitstrg.h"
  17.  
  18. /* constant definition */
  19.  
  20. #define BITLIMIT 450000            /* max number of bits */
  21.  
  22. /*
  23.     print bytes of an array of bits
  24.  
  25.     Warning : the elements of the array are considered 1 byte long
  26.         ( printf("%02X",ptrbit[ind-1]) )
  27.  
  28. */
  29. void printbitarray(ptr)
  30.     struct SPARRAY * ptr;        /* pointer on structure */
  31. {
  32.  
  33.     unsigned ind;
  34.  
  35.     ELEBAR*    ptrbit;
  36.  
  37.     printf("\n");
  38.     ind = (((long)ptr->numlbit + 1) / SIZE)
  39.          + ((((long)ptr->numlbit + 1) & (SIZE - 1)) ? 1 : 0);
  40.  
  41.     for (ptrbit = ptr -> pntarray ;ind > 0;--ind) {
  42.         printf(" %02X",ptrbit[ind-1]);    
  43.     }
  44. }
  45.  
  46. main ()
  47. {
  48.  
  49.     unsigned long    value;
  50.     unsigned long    bitnumb;
  51.     
  52.     int            ch;
  53.  
  54.     unsigned char*    ptr;
  55.  
  56.     LBITARRAY    array;
  57.  
  58.     printf("\nPlease enter number of bits needed [1 to %lu] : ",BITLIMIT);
  59.     scanf("%lu",&value);
  60.     getchar();
  61.  
  62. #ifdef debug
  63.     printf("BITLIMIT = %lu  value = %lu \n",BITLIMIT,value);
  64. #endif
  65.  
  66.     if ( 0 < value && value <= BITLIMIT) {
  67.  
  68.                 /* bit array generation */
  69.         if((array = lcrebitarray(value-1)) == NULL) {
  70.             printf("\nout of memory\n");
  71.             exit(1);
  72.         }
  73.         while(1) {
  74.             printf("\n\nCMD> ");            /* ask for command */
  75.             ch = getchar();                    /* obtain user command */
  76.             getchar();
  77.             switch(tolower(ch)) {
  78.                 case 'c' :                    /* reset bit */
  79.                         printf("\nreset specified bit\n");
  80.                         printf("\nbit number [0 to %lu] : ",value-1);
  81.                         scanf("%lu",&bitnumb);
  82.                         getchar();
  83.                         if(!lclearbit(array,bitnumb)) {
  84.                             printf("bit out of map\n");
  85.                         }
  86.                         else {
  87.                             printf("\nbit %lu is at %ul\n",bitnumb
  88.                                 ,(ltestbit(array,bitnumb)==0) ? 0 : 1);
  89.                         }
  90.                         break;
  91.                 case 'd' :                    /* declare a memory field as
  92.                                                 an array of bits */
  93.                         lfreebitarray(array);
  94.                         printf("\ndeclare a memory field\n");
  95.                         printf("\nnumber of unsigned [1 to %lu] : "
  96.                                 ,(BITLIMIT)/SIZE);
  97.                         scanf("%lu",&bitnumb);
  98.                         getchar();
  99.  
  100.                         if((ptr = (unsigned char*) calloc(bitnumb
  101.                                 ,sizeof(unsigned char))) == NULL) {
  102.                             printf("\nout of memory\n");
  103.                         }
  104.                         else {
  105.                             if((array = ldecarbit(ptr,bitnumb)) == NULL) {
  106.                                 printf("\nout of memory\n");
  107.                             }
  108.                             else {
  109.                                 value = bitnumb * SIZE;
  110.                             }
  111.                         }
  112.                         break;
  113.                 case 'e' :                    /* exit to system */
  114.                         exit (0);
  115.                         break;
  116.                 case 'i' :                    /* invert bit */
  117.                         printf("\ninvert specified bit\n");
  118.                         printf("\nbit number [0 to %lu] : ",value-1);
  119.                         scanf("%lu",&bitnumb);
  120.                         getchar();
  121.                         if(!linvertbit(array,bitnumb)) {
  122.                             printf("bit out of map\n");
  123.                         }
  124.                         else {
  125.                             printf("\nbit %lu is at %ul\n",bitnumb
  126.                                 ,(ltestbit(array,bitnumb)==0) ? 0 : 1);
  127.                         }
  128.                         break;
  129.                 case 's' :                    /* set bit */
  130.                         printf("\nset specified bit\n");
  131.                         printf("\nbit number [0 to %lu] : ",value-1);
  132.                         scanf("%lu",&bitnumb);
  133.                         getchar();
  134.                         if(!lsetbit(array,bitnumb)) {
  135.                             printf("bit out of map\n");
  136.                         }
  137.                         else {
  138.                             printf("\nbit %lu is at %ul\n",bitnumb
  139.                                 ,(ltestbit(array,bitnumb)==0) ? 0 : 1);
  140.                         }
  141.                         break;
  142.                 case 'p' :                    /* print array */
  143.                         printbitarray(array);
  144.                         break;
  145.                 case 'r' :                    /* clear all bits */
  146.                         printf("\nclear all bits\n");
  147.                         lclrbitarray(array);
  148.                         break;
  149.                 case 't' :                    /* reset bit */
  150.                         printf("\ntest specified bit\n");
  151.                         printf("\nbit number [0 to %lu] : ",value-1);
  152.                         scanf("%lu",&bitnumb);
  153.                         getchar();
  154.                         printf("\nbit %lu is at %ul\n",bitnumb
  155.                             ,(ltestbit(array,bitnumb)==0) ? 0 : 1);
  156.                         break;
  157.                 default :
  158.                         printf(" %c, unknown command",ch);
  159.                         printf("\n Usage C-clear, E-exit, I-invert, P-print, R-reset, S-set, T-test\n");
  160.                         break;
  161.             }
  162.         }
  163.     }    
  164.     else {
  165.         printf("\n Value entered is out of range.\n");
  166.     }
  167. }
  168.  
  169.